<?php
defined('_JEXEC') or die( 'Restricted access' );

class TableEnvironmentRecord extends Fcore
{	
	/**
	 * @var int 
	 */
	public $id				= 0;
	
	/**
	 * 观测站ID
	 * @var int 
	 */
	public $oid				= 0;
	
	/**
	 * 记录日期
	 * @var datetime 
	 */
	public $datetime		= '0000-00-00 00:00:00';
	
	/**
	 * 气温
	 * @var string 
	 */
	public $temperature		= '';

	/**
	 * 降水量
	 * @var string
	 */
	public $precipitation	= '';

	/**
	 * 日照
	 * @var string 
	 */
	public $sunshine		= '';
	
	/**
	 * 风向风速
	 * @var string 
	 */
	public $wind			= '';
	
	/**
	 * ph值
	 * @var string 
	 */
	public $ph				= '';
	
	/**
	 * 成分分析
	 * @var string 
	 */
	public $element			= '';
	
	function __construct( &$db )
	{
		parent::__construct( '#__farm_environment_record', 'id', $db );
	}

	/**
	 * Binds an array to the object
	 * @param 	array	Named array
	 * @param 	string	Space separated list of fields not to bind
	 * @return	boolean
	 */
	function bind( $array, $ignore='' )
	{
		$result = parent::bind( $array );
		// cast properties
		$this->id	= (int) $this->id;
		return $result;
	}
	
	public function eoList($oid)
	{
		$tObser = JTable::getInstance('obser', 'Table');
		
		$oid = intval($oid);
		
		$dObser = $tObser->getRow($oid);
		
		if (empty($dObser)) return array();
		
		$data = $this->getAll("WHERE oid=" . $oid . ' ORDER BY datetime DESC');
		
		if (!$data) return array();

		//var_dump(array_merge($dObser, array('ope' => $data)));die;
		
		return array_merge($dObser, array('ope' => $data));
	}

	/**
	 * Overloaded check function
	 *
	 * @access public
	 * @return boolean
	 * @see JTable::check
	 * @since 1.5
	 */
	function check()
	{
		if (!intval( $this->oid ))
		{
			$this->setError(JText::_( '请选择观测站' ));
			return false;
		}
		
		if (!trim( $this->datetime))
		{
			$this->setError(JText::_( '请指定记录日期' ));
			return false;
		}
		
		if (!trim( $this->temperature ))
		{
			$this->setError(JText::_( '请输入气温' ));
			return false;
		}
		
		if (!trim( $this->precipitation ))
		{
			$this->setError(JText::_( '请输入降水量' ));
			return false;
		}
		
		if (trim( $this->sunshine ) == '')
		{
			$this->setError(JText::_( '请输入日照' ));
			return false;
		}
		
		if (trim( $this->wind ) == '')
		{
			$this->setError(JText::_( '请输入风向风速' ));
			return false;
		}
		
		if (trim( $this->ph ) == '')
		{
			$this->setError(JText::_( '请输入ph值' ));
			return false;
		}
		
		if (trim( $this->element) == '')
		{
			$this->setError(JText::_( '请输入成分分析' ));
			return false;
		}
		return true;
	}
}
